package cn.gogpay.dcb.api.mq;

import cn.gogpay.dcb.api.common.constant.BizConstant;
import cn.gogpay.dcb.api.entity.OrderRecord;
import cn.gogpay.dcb.api.service.RechargeRecordService;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author zhanglong@gogpay.cn
 * @date 2019/12/4 18:58
 */
@Component
@RabbitListener(queues = BizConstant.ORDER_WRITER_OFF)
public class OrderPaidReceiver {
    private Logger logger = LoggerFactory.getLogger(this.getClass().getName());
    @Autowired
    private RechargeRecordService rechargeRecordService;

    @RabbitHandler
    public void process(String message) throws Exception {
        try {
            logger.info("接收订单支付状态改变队列信息:message={}", message);
            JSONObject jsonObject = JSONObject.parseObject(message);
            String payStatus = jsonObject.getString("payStatus");
            String orderNo = jsonObject.getString("orderNo");
            String payType = jsonObject.getString("payType");
            OrderRecord record = rechargeRecordService.findByOrderNo(orderNo);
            record.setPayType(payType);
            record.setPayStatus(payStatus);
            if ("01".equals(payStatus)) {
                record.setStatus("1");
            }
            rechargeRecordService.updateOrder(record);
        }catch (Exception ex){
            logger.warn("处理订单支付状态改变队列信息出错",ex);
        }
    }

}
